package com.cold.source.money.pro.loan.network

import com.gowow.gowoworionapp.network.response.BaseResponse
import com.gowow.gowoworionapp.network.response.DisposableDataCallBack
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
/**
 * @Author ninetailedfox
 * @Data 2023/1/19 9:11 下午
 * @ClassName ServiceUtils
 **/
fun <T> request(api: Single<BaseResponse<T>>, success: (T?) -> Unit = {}, error: (String) -> Unit = {}, isShow: Boolean = false, code: (String) -> Unit = {}) {
//    if (isShow) {
//        WaitDialog.show("")
//    }
    api.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(object : DisposableDataCallBack<T>() {
            override fun onSafeSuccess(data: T?) {
                super.onSafeSuccess(data)
                success(data)
            }

            override fun onSafeFailed(code: String?, message: String?) {
                super.onSafeFailed(code, message)
                if (code != null) {
                    code(code)
                }
                if (message != null) {
                    error(message)
                }

            }
        })
}

fun <T> request2(api: Single<BaseResponse<T>>, success: (T) -> Unit = {}, error: (String) -> Unit = {}, code: (String) -> Unit = {}) {
    api.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeWith(object : DisposableDataCallBack<T>() {
            override fun onSafeSuccess(data: T?) {
                super.onSafeSuccess(data)
                if (data != null) {
                    success(data)
                }
            }

            override fun onSafeFailed(code: String?, message: String?) {
                super.onSafeFailed(code, message)
                if (code != null) {
                    code(code)
                }
                if (message != null) {
                    error(message)
                }

            }
        })
}
